﻿using MCommons.MImage;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using WpfApp1.Dto;
using WpfApp1.Models;

namespace WpfApp1
{
    /// <summary>
    /// DataGridWpf.xaml 的交互逻辑
    /// </summary>
    public partial class DataGridWpf : Window
    {
        public DataGridWpf()
        {
            InitializeComponent();

            _datas = new ObservableCollection<UploadViewModel>();
            dataGrid.CanUserSortColumns = false;
            InitData();
        }

        private void InitData()
        {
            var datas = new List<UploadFamilyInfo>();
            datas.Add(new UploadFamilyInfo()
            {
                IsSelect = true,
                Name = "测试使用",
                PicUrl = @"E:\Pic\12.jpg",
                Type = ""
            });
            datas.Add(new UploadFamilyInfo()
            {
                IsSelect = true,
                Name = "测试使用1",
                PicUrl = @"E:\Pic\12.jpg",
                Type = ""
            });
            datas.Add(new UploadFamilyInfo()
            {
                IsSelect = true,
                Name = "测试使用2",
                PicUrl = @"E:\Pic\12.jpg",
                Type = ""
            });
            datas.Add(new UploadFamilyInfo()
            {
                IsSelect = true,
                Name = "测试使用3",
                PicUrl = @"E:\Pic\12.jpg",
                Type = ""
            });
            datas.Add(new UploadFamilyInfo()
            {
                IsSelect = false,
                Name = "测试使用4",
                PicUrl = @"E:\Pic\12.jpg",
                Type = ""
            });

            datas.ForEach(e => _datas.Add(new UploadViewModel(e)));

            while (_datas.Count < 9)
            {
                _datas.Add(new UploadViewModel());
            }

        }

        public void InitGridData()
        {
   
            dataGrid.ItemsSource = _datas;

        }

        private const int imageCellIndex = 3;   //图片 列索引
        private const int typeCellIndex = 4;    //类型索引

        private ObservableCollection<UploadViewModel> _datas;

        /// <summary>
        /// 族文件 或 族文件夹路径
        /// </summary>
        private string filePath;

        /// <summary>
        /// 1 文件  2 为文件夹
        /// </summary>
        private int opentype = 1;

        private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Row_DoubleClick");
            var cells = dataGrid.SelectedCells;
            if (cells.Any())
            {
                var rowIndex = dataGrid.Items.IndexOf(cells.First().Item);
                var columnIndex = cells.First().Column.DisplayIndex;
                switch(columnIndex)
                {
                    case imageCellIndex:
                        {
                            //MessageBox.Show($"{rowIndex},{columnIndex}");
                            Pic_SelectAction(rowIndex);
                            break;
                        }
                    case typeCellIndex:
                        {
                            Type_SelectAction(rowIndex);
                            break;
                        }
                    default:
                        break;
                }
                //MessageBox.Show($"{rowIndex},{columnIndex}");
            }
        }

        /// <summary>
        /// 图片选择
        /// </summary>
        private void Pic_SelectAction(int rowIndex)
        {
            Microsoft.Win32.OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.jpg|*.jpg";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                if (!string.IsNullOrEmpty(dlg.FileName))
                {
                    _datas[rowIndex].PicProperty = ImageUtils.ConvertBitmap(ImageUtils.CreateImage(dlg.FileName, 80, 100));
                }
            }
        }

        /// <summary>
        /// 类型选择
        /// </summary>
        /// <param name="rowIndex"></param>
        private void Type_SelectAction(int rowIndex)
        {
            MessageBox.Show("类型选择");
            var dlg = new TreeViewDemo();
            var rest = dlg.ShowDialog();
            if(rest != null)
            {
                var tt = dlg.DialogResult.Value;
                if (rest.Value)
                {
                    _datas[rowIndex].Type = dlg.SelectProduct.CategoryName;
                }
            }
        }

        private void Btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            var cells = dataGrid.SelectedCells;
            if (cells.Any())
            {
                var rowIndex = dataGrid.Items.IndexOf(cells.First().Item);
                var rest = MessageBox.Show("是否确认删除", "操作提示", MessageBoxButton.YesNo);
                if (rest == MessageBoxResult.Yes)
                {
                    MessageBox.Show($"删除成功{rowIndex}");
                    _datas.RemoveAt(rowIndex);
                }
            }
        }

        private void Btn_Upload_Click(object sender, RoutedEventArgs e)
        {
            var cells = dataGrid.SelectedCells;
            if (cells.Any())
            {
                var rowIndex = dataGrid.Items.IndexOf(cells.First().Item);
                var rest = MessageBox.Show("是否确定上传","操作提示",MessageBoxButton.YesNo);
                if(rest == MessageBoxResult.Yes)
                {
                    MessageBox.Show($"上传成功{rowIndex}");
                }
                //_datas.RemoveAt(rowIndex);
            }
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            var btn = (Button)e.Source;
            if (btn is null)
                return;

            switch (btn.Name)
            {
                case "OpenFile":
                    Btn_OpenFile();
                    InitGridData();
                    opentype = 1;
                    break;
                case "OpenDic":
                    Btn_OpenFolder();
                    break;
                case "AllSelect":
                    btn_SelectAllAction();
                    break;
                case "InvSelect":
                    btn_SelectInvAction();
                    break;
                case "OK_Upload":
                    btn_OK_UploadAction();
                    break;
                case "Cancle":
                    btn_Cancle();
                    break;
                default:
                    MessageBox.Show("Btn_Click error！");
                    break;
            }
        }

        private void Btn_OpenFile()
        {
            Microsoft.Win32.OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.rft|*.rft";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                filePath = dlg.FileName;
                FilePath.Text = dlg.FileName;
            }
        }

        private void Btn_OpenFolder()
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择族文件所在文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    System.Windows.MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
            }
            MessageBox.Show(dialog.SelectedPath);
        }

        private void FilePath_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                //dataGrid.Columns.Clear();
                dataGrid.Items.Clear();
            }
        }

        private void btn_SelectAllAction()
        {
            _datas.Where(e => !string.IsNullOrEmpty(e.Name)).ToList().ForEach(e => e.IsSelectProperty = true);
        }

        private void btn_SelectInvAction()
        {
            var selects =  _datas.Where(e => e.IsSelectProperty == true && !string.IsNullOrEmpty(e.Name)).ToList();
            _datas.Where(e => e.IsSelectProperty == false && !string.IsNullOrEmpty(e.Name)).ToList().ForEach(e => e.IsSelectProperty = true);
            _datas.Where(e => selects.Contains(e) ).ToList().ForEach(e => e.IsSelectProperty = false);
        }

        private void btn_OK_UploadAction()
        {
            MessageBox.Show("OK_Upload btn click");
        }

        private void btn_Cancle()
        {
            _datas.Clear();
            //dataGrid.ItemsSource = null;
        }

        private void CheckClick(object sender, RoutedEventArgs e)
        {
            var cells = dataGrid.SelectedCells;
            if (cells.Any())
            {
                var obj = (CheckBox)sender;
                if(obj != null)
                {
                    var rowIndex = dataGrid.Items.IndexOf(cells.First().Item);
                    _datas[rowIndex].IsSelectProperty = obj.IsChecked.Value;
                }
               
                //_datas[rowIndex].IsSelectProperty;
            }
        }
    }

}
